package com.xzy.demo.juc.locks;

import java.util.concurrent.TimeUnit;

/**
 * @author xzy
 * @date 2021/3/21 19:58
 * 说明：
 */
public class Lock8Demo {
    public static void main(String[] args) throws Exception {
        Phone phone = new Phone();
        Phone phone2 = new Phone();

        new Thread(() -> {
            try {
                phone.sendEmail();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "A").start();
        TimeUnit.SECONDS.sleep(1);

        new Thread(() -> {
            phone.sayHello();
        }, "B").start();
    }
}

class Phone {
    public synchronized void sendEmail() throws InterruptedException {
        TimeUnit.SECONDS.sleep(4);
        System.out.println(Thread.currentThread().getName() + " 发邮件");
    }

    public synchronized void sendSMS() {
        System.out.println(Thread.currentThread().getName() + " 发短信");
    }

    public void sayHello() {
        System.out.println(Thread.currentThread().getName() + " sayHello");
    }
}